package com.scs.application.modules.dept.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.dept.dto.DemandItemVo;
import com.scs.application.modules.dept.entity.Demand;
import com.scs.application.modules.dept.entity.DemandItem;
import com.scs.application.modules.dept.entity.DemandSum;
import com.scs.application.modules.dept.entity.DemandSumItem;
import com.scs.application.modules.dept.mapper.DemandSumItemMapper;
import com.scs.application.modules.dept.service.DemandItemService;
import com.scs.application.modules.dept.service.DemandService;
import com.scs.application.modules.dept.service.DemandSumItemService;
import com.scs.application.modules.dept.service.DemandSumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class DemandSumItemServiceImpl extends BaseServiceImpl<DemandSumItemMapper, DemandSumItem> implements DemandSumItemService {
    @Autowired
    private DemandSumItemMapper demandSumItemMapper;
    @Autowired
    private DemandService demandService;
    @Autowired
    private DemandItemService demandItemService;
    @Autowired
    private DemandSumService demandSumService;

    @Override
    public boolean removeCascadeByIds(Collection<? extends Serializable> idList) {
        List<DemandSumItem> sumItems = demandSumItemMapper.selectList(Wrappers.<DemandSumItem>query().in("id", idList));

        boolean success = super.removeCascadeByIds(idList);
//        更新回待审核状态
        sumItems.forEach(item ->{
            demandService.update(Wrappers.<Demand>update().set("flow_status", "submit").eq("id", item.getDemandId()));
        });
        //        改金额
        Set<String> ids = sumItems.stream().map(item -> item.getDemandSumId()).collect(Collectors.toSet());
        List<DemandSumItem> demandItems = demandSumItemMapper.selectList(Wrappers.<DemandSumItem>query().in("demand_sum_id", ids));
        List<String> collect = demandItems.stream().map(item -> item.getDemandId()).collect(Collectors.toList());
        Double aDouble = updateDemandSumAmount(collect);
        for (DemandSumItem sumItem : sumItems) {
            demandSumService.update(Wrappers.<DemandSum>update().set("amount", aDouble).eq("id", sumItem.getDemandSumId()));
        }


        return success;
    }
    private Double updateDemandSumAmount(List<String> idList) {
        if (idList.size()==0){
            return 0.0;
        }
        List<DemandItem> demandItems = demandItemService.list(Wrappers.<DemandItem>query().select("amount").in("demand_id", idList));
        return demandItems.stream().mapToDouble(o -> o.getAmount()).sum();
    }


    @Override
    public IPage<DemandItemVo> pageAll(QueryContext context) {
        return demandSumItemMapper.pageAll(context.getPage(), context.getWrapper(), context.getParams());

    }
}
